package com.audaque.springboot.foshanupload.casclientautoconfigsupport.config;

import com.audaque.springboot.foshanupload.casclientautoconfigsupport.properties.ServerProperties;
import com.audaque.springboot.foshanupload.casclientautoconfigsupport.properties.ServiceProperties;
import com.audaque.springboot.foshanupload.casclientautoconfigsupport.strategy.AuthenticationIgnoreUrlPatternTypeMatcherStrategy;
import com.audaque.springboot.foshanupload.casclientautoconfigsupport.strategy.UnAuthenticationRedirectStrategy;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.core.properties.StaticResourcePathProperties;
import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * cas过滤器注入：方式2  完全重新注入新的过滤器
 */
@Configuration
public class CasCoreFilterConfig {
    @Autowired
    private ServerProperties serverProperties;
    @Autowired
    private ServiceProperties serviceProperties;

    @Autowired
    private StaticResourcePathProperties staticResourcePathProperties;


    /**
     * 单点登录和登出会话管理过滤器：服务端处理
     * 作用：登录是保存session,登出时删除session
     * SingleSignOutFilter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean filterSingleRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SingleSignOutFilter());
        // 设定匹配的路径
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerUrlPrefix", serverProperties.getServerUrlPrefix());
        registration.setInitParameters(initParameters);
        // 设定加载的顺序：这个过滤器要放到最前面，否则网站退出功能失效
        registration.setOrder(1);
        return registration;
    }


    /**
     * cas验票过滤器：客户端与服务端处理
     * Cas30ProxyReceivingTicketValidationFilter
     * description:过滤验证器
     * * @param: []
     *
     * @return: org.springframework.boot.web.servlet.FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean filterValidationRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        // 设定匹配的路径
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerUrlPrefix", serverProperties.getServerUrlPrefix());
        initParameters.put("serverName", serverProperties.getClientHostUrl());
        //使用session机制
        initParameters.put("useSession", "true");
        //不加密回调参数来实现回调时响应json
        initParameters.put("encodeServiceUrl", "false");
        registration.setInitParameters(initParameters);
        registration.setOrder(2);
        return registration;
    }


    /**
     * 认证过滤器：负责在登录时重定到服务器页面
     * description:授权过滤器：客户端与服务端处理
     *
     * @param: []
     * @return: org.springframework.boot.web.servlet.FilterRegistrationBean
     */

    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        // 设定匹配的路径
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerLoginUrl", serverProperties.getServerLoginUrl());
        initParameters.put("serverName", serverProperties.getClientHostUrl());
        //忽略认证的路径
        String ignorePattern = StringUtils.join(staticResourcePathProperties.getList(), "|");
        String dynamicResourceIgnorePattern = serviceProperties.getDynamicResourceIgnorePattern();
        ignorePattern = ignorePattern + "|" + dynamicResourceIgnorePattern;
        initParameters.put("ignorePattern", ignorePattern);
        //忽略认证路径的类
        initParameters.put("ignoreUrlPatternType", AuthenticationIgnoreUrlPatternTypeMatcherStrategy.class.getName());

        initParameters.put("authenticationRedirectStrategyClass", UnAuthenticationRedirectStrategy.class.getName());


        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(3);
        return registration;
    }


}
